package cn.fangline.wo.service.impl;

import cn.fangline.base.util.LogicUtil;
import cn.fangline.wo.mapper.HouseImgMapper;
import cn.fangline.wo.mapper.HouseMapper;
import cn.fangline.wo.model.House;
import cn.fangline.wo.model.HouseImg;
import cn.fangline.wo.model.Section;
import cn.fangline.wo.model.User;
import cn.fangline.wo.service.IHouseService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * <p>
 * 房源表 服务实现类
 * </p>
 *
 * @author 抽离
 * @since 2017-04-21
 */
@Service
public class HouseServiceImpl extends ServiceImpl<HouseMapper, House> implements IHouseService {

    DecimalFormat df = new DecimalFormat("#########0"); //四色五入转换成整数

    @Autowired
    HouseImgMapper houseImgMapper;

    @Override
    public Long saveHouse(Long userId,
                          String section,Integer room,Integer hall,Integer toilet,
                          Float area,Float totalPrice, Integer floor,Integer totalFloor,
                          String towardsItems,String tagItems,Integer template,
                          String[] serverImages,
                          Integer shareCount,Integer transCount){

        final User user = new User().selectOne(
                new EntityWrapper<User>().eq("user_id", userId));
        user.setStatus(1);
        user.updateById();

        House house = new House();
        house.setCreateTime(new Date());

        if(StringUtils.isNotBlank(section)){
            Section s = new Section().selectOne(new EntityWrapper<Section>().eq("section_name",section));
            if(s!=null){
                house.setAddress(s.getDistrict()+","+s.getArea());
            }
        }

        house.setSection(section);
        house.setRoom(room);
        house.setHall(hall);
        house.setToilet(toilet);
        house.setArea(area);
        house.setTotalPrice(totalPrice);
//        house.setShareCount(shareCount);
//        house.setTransCount(transCount);
        if(area!=null && totalPrice!=null){
            house.setUnitPrice(Integer.parseInt(df.format(totalPrice*10000/area)));
        }
        if(LogicUtil.isReal(serverImages) && serverImages.length>0){
            house.setImgUrl(serverImages[0]);
        }
//        house.setTemplate(template);
        house.setCreateUser(userId);
        house.setCityId(user.getCityId());
        house.setCorpId(user.getCorpId());
        house.setTowards(towardsItems);
        house.setFloor(floor+"/"+totalFloor);
        house.insert();

//        HouseDetail houseDetail = new HouseDetail();
//        houseDetail.setHouseId(house.getHouseId());
//        houseDetail.setFloor(floor);
//        houseDetail.setTotalFloor(totalFloor);
//        houseDetail.setTowards(towardsItems);
//        houseDetail.setTags(tagItems);
//        houseDetail.setCityId(user.getCityId());
//        houseDetail.insert();



        if(LogicUtil.isReal(serverImages) && serverImages.length>0){
            for(int i=0;i<serverImages.length;i++){
                HouseImg h = new HouseImg();
                h.setHouseId(house.getHouseId());
                h.setImgUrl(serverImages[i]);
                h.setUploadTime(new Date());
                h.setCityId(user.getCityId());
                h.insert();
            }
        }


        return house.getHouseId();
    }

    @Override
    public void updateHouse(Long houseId,
                            Long userId,
                            String section,Integer room,Integer hall,Integer toilet,
                            Float area,Float totalPrice, Integer floor,Integer totalFloor,
                            String towardsItems,String tagItems,Integer template,
                            String[] serverImages,
                            Integer shareCount,Integer transCount){
        final House house = new House().selectById(houseId);
        if(StringUtils.isNotBlank(section) && !section.equals(house.getSection())){
            Section s = new Section().selectOne(new EntityWrapper<Section>().eq("section_name",section));
            if(s!=null){
                house.setAddress(s.getDistrict()+","+s.getArea());
            }
        }
//        house.setHouseId(houseId);
        house.setCreateTime(new Date());

        house.setSection(section);
        house.setRoom(room);
        house.setHall(hall);
        house.setToilet(toilet);
        house.setArea(area);
        house.setTotalPrice(totalPrice);
        house.setTowards(towardsItems);
        house.setFloor(floor+"/"+totalFloor);
//        house.setTransCount(transCount);
//        house.setShareCount(shareCount);
        if(area!=null && totalPrice!=null){
            house.setUnitPrice(Integer.parseInt(df.format(totalPrice*10000/area)));
        }
        if(LogicUtil.isReal(serverImages) && serverImages.length>0){
            house.setImgUrl(serverImages[0]);
        }
//        house.setTemplate(template);
        house.setCreateUser(userId);
        house.updateById();

//        final HouseDetail houseDetail = new HouseDetail();
//        houseDetail.setHouseId(houseId);
//        houseDetail.setFloor(floor);
//        houseDetail.setTotalFloor(totalFloor);
//        houseDetail.setTowards(towardsItems);
//        houseDetail.setTags(tagItems);
//        houseDetail.updateById();

        houseImgMapper.deleteByMap(new HashMap<String, Object>(){{
            put("house_id",house.getHouseId());
        }});

        if(LogicUtil.isReal(serverImages) && serverImages.length>0){
            for(int i=0;i<serverImages.length;i++){
                HouseImg h = new HouseImg();
                h.setHouseId(house.getHouseId());
                h.setImgUrl(serverImages[i]);
                h.setUploadTime(new Date());
                h.insert();
            }
        }
    }
}
